<?php

/* ----------------------------------------------
  Caloris: Administration Store

  $Id$
  $Author$

  List of modules
  ---------------------------------------------- */

require_once("../../../index.php");
Caloris\ACL::authenticate();

define('_MODULE_ACTIVE', 'store');

if ($_REQUEST["function_name"] == 'store_module_install') {

    if ((Caloris\ACL::authorize("modules") != 1)) {
        header("location: " . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . "/" . _MODULE_ACTIVE . "/index.php?msg=" . urlencode("Nemáte oprávnění"));
        exit();
    } else {
        // curl download
        if (file_exists(_CALSET_PATHS_BASE . _CALSET_DIRS_MODULE . '/' . $_POST["file"])) {
            header("location: " . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . "/" . _MODULE_ACTIVE . "/index.php?msg=" . urlencode("Adresář s názvem modulu existuje"));
            exit();
        }

        if (file_exists(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/caloriscms-' . $_POST["file"] . '.zip')) {
            header("location: " . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . "/" . _MODULE_ACTIVE . "/index.php?msg=" . urlencode("Tento soubor nebo soubor se stejným jménem existuje"));
            exit();
        }
        
        $url = 'http://caloris.cz/store/modules/caloriscms-' . $_POST["file"] . '.zip';
        $path = _CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/caloriscms-' . $_POST["file"] . '.zip';

        $ch = curl_init($url);
        if (!$ch) {
            header("location: " . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . "/" . _MODULE_ACTIVE . "/index.php?msg=" . urlencode("Problém s přenosem souboru"));
            exit();
        }

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $data = curl_exec($ch);
        curl_close($ch);
        file_put_contents($path, $data);

        if ($_POST["j"] != 1) {
            header("location: " . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . "/modulemanager/index.php?function_name=module_install&type=transfer&file=caloriscms-" . $_POST["file"] . '.zip');
            exit();
        } else {
            echo "{\"msg\": \"done\"}";
            exit();
        }
    }
}

$content = '
<p class="perex" style="color: red;" id="msg">[cal:predefs(msg) /]</p>

<h1>Store: Moduly</h1>
<p style="padding-bottom: 20px;">Moduly jsou funkce rozšiřující možnosti stránek.</p>

<h2>Seznam modulů</h2>
';

$ch = curl_init(); // create cURL handle (ch)

if (!$ch) {
    die("Couldn't initialize");
}

// set some cURL options
$ret = curl_setopt($ch, CURLOPT_URL, "http://caloris.cz/store/caloris_modules.xml");
$ret = curl_setopt($ch, CURLOPT_HEADER, false);
$ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ret = curl_setopt($ch, CURLOPT_NOBODY, 0);
$ret = curl_setopt($ch, CURLOPT_TIMEOUT, 40);

// execute
$ret = curl_exec($ch);

if (empty($ret)) {
    die(curl_error($ch));
    curl_close($ch); // close cURL handler
} else {
    $info = curl_getinfo($ch);
    curl_close($ch); // close cURL handler

    if (empty($info['http_code'])) {
        die("No HTTP code was returned");
    } else {
        $xmlModulesS = simplexml_load_string($ret);
    }
}

$xma = $xmlModulesS;

if (count($xma->modules->children()) > 0) {
    $content .= '
<table class="table table-striped">
    <tr>
        <th>Název modulu</th>
        <th>Popis</th>
        <th style="width: 80px;"></th>
    </tr>
';
    foreach ($xma->modules->children() as $child) {
        // Smazání extenze, u které není soubor
        $gname = $child->getName();

        if (!file_exists(_CALSET_PATHS_BASE . _CALSET_DIRS_MODULE . '/' . $xma->modules->{$gname}->name)) {
            $system = '
<form action="' . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/' . _MODULE_ACTIVE . '/index.php" method="post">
<input type="hidden" name="function_name" value="store_module_install" />
<input type="hidden" name="checksum" value="' . $xma->modules->{$gname}->checksum . '" />
<input type="hidden" name="file" value="' . $gname . '" />
<input type="submit" value="Nainstalovat" class="btn btn-success" />
</form>';
        } else {
            $system = '<en>Nainstalováno</em>';
        }

        $content .= '
    <tr id="trbl_' . $gname . '" >
        <td>' . $xma->modules->{$gname}->name . '</td>
        <td>' . $xma->modules->{$gname}->description . '</td>
        </td>
        <td>' . $system . '</td>
    </tr>';
    }

    $content .= '
</table>';
} else {
    $content .= '<p>Žádné moduly nenalezeny</p>';
}

$data["title"] = 'Store: Moduly';

$tmpl = new Caloris\Template;
$tmpl->input($content);
$tmpl->renderHead($data);
echo $tmpl->renderBody('caloris-admin');